package utils;


import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


public class SqlSessionFactoryUtil {
	private static SqlSessionFactory factory=null;
	private static SqlSession session=null;
	private SqlSessionFactoryUtil(){
		
	}
	public synchronized static SqlSessionFactory initSqlSessionFactory(){
		if(factory==null){
			String resource="mybatis-config.xml";
			InputStream inputStream = null;
			try {
				inputStream = Resources.getResourceAsStream(resource);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			factory=(new SqlSessionFactoryBuilder()).build(inputStream);
			//factory.getConfiguration().addMapper(RoleMapper.class);
		}
		return factory;
	}
	public synchronized static SqlSession openSession(){
		if(factory==null){
			initSqlSessionFactory();
		}
		return factory.openSession();
	}

}
